package com.lxmes.sales.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.ZcBidNotice;
import com.lxmes.sales.mapper.ZcBidNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.ZcQuotationOverviewMapper;
import com.lxmes.sales.domain.ZcQuotationOverview;
import com.lxmes.sales.service.IZcQuotationOverviewService;

/**
 * 报价一览Service业务层处理
 *
 * @author lx
 * @date 2023-06-21
 */
@Service
public class ZcQuotationOverviewServiceImpl implements IZcQuotationOverviewService {
    @Autowired
    private ZcQuotationOverviewMapper zcQuotationOverviewMapper;

    @Autowired
    private ZcBidNoticeMapper zcBidNoticeMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询报价一览
     *
     * @param id 报价一览主键
     * @return 报价一览
     */
    @Override
    public ZcQuotationOverview selectZcQuotationOverviewById(Long id) {
        return zcQuotationOverviewMapper.selectZcQuotationOverviewById(id);
    }

    /**
     * 查询报价一览列表
     *
     * @param zcQuotationOverview 报价一览
     * @return 报价一览
     */
    @Override
    public List<ZcQuotationOverview> selectZcQuotationOverviewList(ZcQuotationOverview zcQuotationOverview) {
        return zcQuotationOverviewMapper.selectZcQuotationOverviewList(zcQuotationOverview);
    }

    /**
     * 新增报价一览
     *
     * @param zcQuotationOverview 报价一览
     * @return 结果
     */
    @Override
    public int insertZcQuotationOverview(ZcQuotationOverview zcQuotationOverview) {
        return zcQuotationOverviewMapper.insertZcQuotationOverview(zcQuotationOverview);
    }

    /**
     * 修改报价一览
     *
     * @param zcQuotationOverview 报价一览
     * @return 结果
     */
    @Override
    public int updateZcQuotationOverview(ZcQuotationOverview zcQuotationOverview) {
        return zcQuotationOverviewMapper.updateZcQuotationOverview(zcQuotationOverview);
    }

    /**
     * 批量删除报价一览
     *
     * @param ids 需要删除的报价一览主键
     * @return 结果
     */
    @Override
    public int deleteZcQuotationOverviewByIds(Long[] ids) {
        return zcQuotationOverviewMapper.deleteZcQuotationOverviewByIds(ids);
    }

    /**
     * 删除报价一览信息
     *
     * @param id 报价一览主键
     * @return 结果
     */
    @Override
    public int deleteZcQuotationOverviewById(Long id) {
        return zcQuotationOverviewMapper.deleteZcQuotationOverviewById(id);
    }

    @Override
    public String imporIteminfo(List<ZcQuotationOverview> zcQuotationOverviewList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcQuotationOverviewList) || zcQuotationOverviewList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcQuotationOverview zcQuotationOverview : zcQuotationOverviewList) {
            try {
                BeanValidators.validateWithException(validator, zcQuotationOverview);
                zcQuotationOverview.setCreateBy(operName);
                zcQuotationOverview.setUpdateBy(operName);
                this.insertZcQuotationOverview(zcQuotationOverview);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int confirmZcQuotationOverview(ZcQuotationOverview zcQuotationOverview) {
        List<ZcQuotationOverview> zcQuotationOverviews = zcQuotationOverviewMapper.selectZcQuotationOverviewByOrderNumber(zcQuotationOverview.getOrderNumber());
        zcQuotationOverviews.stream().filter(i -> ObjectUtil.equal(i.getConfirmStatus(), "0") && ObjectUtil.notEqual(i.getId(), zcQuotationOverview.getId())).forEach(i -> {
            i.setConfirmStatus("2");
            zcQuotationOverviewMapper.updateZcQuotationOverview(i);
        });
        ZcQuotationOverview zcQuotationOverview1 = zcQuotationOverviews.stream().filter(i -> ObjectUtil.equal(i.getId(), zcQuotationOverview.getId())).findFirst().get();
        zcQuotationOverview1.setConfirmStatus("1");
        zcQuotationOverviewMapper.updateZcQuotationOverview(zcQuotationOverview1);
        ZcBidNotice zcBidNotice = Convert.convert(ZcBidNotice.class, zcQuotationOverview1);
        zcBidNotice.setBidStatus("1");
        zcBidNotice.setBidDateTime(new Date());
        zcBidNotice.setDeptId(SecurityUtils.getDeptId());
        zcBidNoticeMapper.insertZcBidNotice(zcBidNotice);
        return 1;
    }
}
